// Copyright (C)2012 Johann Duscher. All rights reserved.
//
// This file is part of QwTAPI.
//
// QwTAPI is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// QwTAPI is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with QwTAPI.  If not, see <http://www.gnu.org/licenses/>.

#include "LineListener.h"

#include "Line.h"


namespace qwtapi
{

LineListener::LineListener(QObject* pParent)
    : super(pParent)
{
}

void LineListener::onClose()
{
    qDebug() << Q_FUNC_INFO << getLineHandle();
}

void LineListener::onDynamicCreate()
{
    qDebug() << Q_FUNC_INFO << getLineHandle();
}

void LineListener::onDynamicRemove()
{
    qDebug() << Q_FUNC_INFO << getLineHandle();
}

void LineListener::onLineEvent(DWORD hDevice, Message::Constant msg, DWORD dwParam1, DWORD dwParam2, DWORD dwParam3)
{
    switch (msg)
    {
    case Message::ADDRESSSTATE:
        if (getLineHandle() == static_cast<HLINE>(hDevice))
            onAddressState(dwParam1, static_cast<AddressState::Constants>(dwParam2));
        break;
    case Message::APPNEWCALL:
        Q_ASSERT(getLineHandle() == static_cast<HLINE>(hDevice));
        onAppNewCall(dwParam1, static_cast<HCALL>(dwParam2), static_cast<CallPrivilege::Constant>(dwParam3));
        break;
    case Message::CALLINFO:
        onCallInfoState(static_cast<HCALL>(hDevice), static_cast<CallInfoState::Constants>(dwParam1));
        break;
    case Message::CALLSTATE:
        onCallState(static_cast<HCALL>(hDevice), static_cast<CallState::Constant>(dwParam1), dwParam2, static_cast<CallPrivilege::Constant>(dwParam3));
        break;
    case Message::DEVSPECIFIC:
        onDevSpecific(hDevice, dwParam1, dwParam2, dwParam3);
        break;
    case Message::DEVSPECIFICFEATURE:
        onDevSpecificFeature(hDevice, dwParam1, dwParam2, dwParam3);
        break;
    case Message::GATHERDIGITS:
        onGatherDigits(static_cast<HCALL>(hDevice), static_cast<GatherTerm::Constant>(dwParam1), dwParam3);
        break;
    case Message::GENERATE:
        onGenerate(static_cast<HCALL>(hDevice), static_cast<GenerateTerm::Constant>(dwParam1), dwParam3);
        break;
    case Message::LINEDEVSTATE:
        Q_ASSERT(getLineHandle() == static_cast<HLINE>(hDevice));
        onLineDevState(static_cast<LineDevState::Constants>(dwParam1), dwParam2, dwParam3);
        break;
    case Message::MONITORDIGITS:
        onMonitorDigits(static_cast<HCALL>(hDevice), dwParam1, static_cast<DigitMode::Constant>(dwParam2), dwParam3);
        break;
    case Message::MONITORMEDIA:
        onMonitorMedia(static_cast<HCALL>(hDevice), static_cast<MediaMode::Constant>(dwParam1), dwParam3);
        break;
    case Message::MONITORTONE:
        onMonitorTone(static_cast<HCALL>(hDevice), dwParam1, dwParam3);
        break;
    default:
        qDebug() << Q_FUNC_INFO << getLineHandle() << "UNKNOWN TAPI MESSAGE:" << hDevice << msg << dwParam1 << dwParam2 << dwParam3;
    }
}

void LineListener::onAppNewCall(DWORD addressId, HCALL hCall, CallPrivilege::Constant privilege)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << addressId << hCall << privilege;
}

void LineListener::onDevSpecific(DWORD hLineOrCall, DWORD devSpecific1, DWORD devSpecific2, DWORD devSpecific3)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hLineOrCall << devSpecific1 << devSpecific2 << devSpecific3;
}

void LineListener::onDevSpecificFeature(DWORD hLineOrCall, DWORD devSpecific1, DWORD devSpecific2, DWORD devSpecific3)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hLineOrCall << devSpecific1 << devSpecific2 << devSpecific3;
}

void LineListener::onGatherDigits(HCALL hCall, GatherTerm::Constant gatherTermination, DWORD tickCount)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << gatherTermination << tickCount;
}

void LineListener::onGenerate(HCALL hCall, GenerateTerm::Constant generateTermination, DWORD tickCount)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << generateTermination << tickCount;
}

void LineListener::onLineDevState(LineDevState::Constants devState, DWORD devStateDetail1, DWORD devStateDetail2)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << devState << devStateDetail1 << devStateDetail2;
}

void LineListener::onMonitorDigits(HCALL hCall, DWORD digit, DigitMode::Constant digitMode, DWORD tickCount)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << digit << digitMode << tickCount;
}

void LineListener::onMonitorMedia(HCALL hCall, MediaMode::Constant mediaMode, DWORD tickCount)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << mediaMode << tickCount;
}

void LineListener::onMonitorTone(HCALL hCall, DWORD appSpecific, DWORD tickCount)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << appSpecific << tickCount;
}


void LineListener::onCallInfoState(HCALL hCall, CallInfoState::Constants callInfoState)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callInfoState;
}


void LineListener::onCallState(HCALL hCall, CallState::Constant callState, DWORD callStateDetail, CallPrivilege::Constant callPrivilege)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callState << callPrivilege;

    switch (callState)
    {
    case CallState::IDLE:
        onCallStateIdle(hCall);
        break;
    case CallState::OFFERING:
        onCallStateOffering(hCall, callStateDetail);
        break;
    case CallState::ACCEPTED:
        onCallStateAccepted(hCall);
        break;
    case CallState::DIALTONE:
        onCallStateDialtone(hCall, callStateDetail);
        break;
    case CallState::DIALING:
        onCallStateDialing(hCall);
        break;
    case CallState::RINGBACK:
        onCallStateRingback(hCall);
        break;     
    case CallState::BUSY:
        onCallStateBusy(hCall, callStateDetail);
        break;
    case CallState::CONNECTED:
        onCallStateConnected(hCall, callStateDetail);
        break;            
    case CallState::PROCEEDING:
        onCallStateProceeding(hCall);
        break;
    case CallState::ONHOLD:
        onCallStateHold(hCall);
        break;
    case CallState::ONHOLDPENDCONF:
        onCallStateHoldPendingConference(hCall);
        break;
    case CallState::ONHOLDPENDTRANSFER:
        onCallStateHoldPendingTransfer(hCall);
        break;
    case CallState::SPECIALINFO:
        onCallStateSpecialInfo(hCall, callStateDetail);
        break;
    case CallState::DISCONNECTED:
        onCallStateDisconnected(hCall, callStateDetail);
        break;
    case CallState::CONFERENCED:
        onCallStateConferenced(hCall);
        break;
    case CallState::UNKNOWN:
        onCallStateUnknown(hCall, callStateDetail, callPrivilege);
        break;
    default:
        qDebug() << Q_FUNC_INFO << getLineHandle() << "UNKNOWN CALL STATE:" << hCall << callState << callStateDetail << callPrivilege;
    }
}

void LineListener::onCallStateIdle(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateOffering(HCALL hCall, DWORD callStateDetail)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callStateDetail;
}

void LineListener::onCallStateAccepted(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateDialtone(HCALL hCall, DWORD callStateDetail)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callStateDetail;
}

void LineListener::onCallStateDialing(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateRingback(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateBusy(HCALL hCall, DWORD callStateDetail)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callStateDetail;
}

void LineListener::onCallStateConnected(HCALL hCall, DWORD callStateDetail)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callStateDetail;
}

void LineListener::onCallStateProceeding(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateHold(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateHoldPendingConference(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateHoldPendingTransfer(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateSpecialInfo(HCALL hCall, DWORD callStateDetail)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callStateDetail;
}

void LineListener::onCallStateDisconnected(HCALL hCall, DWORD callStateDetail)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callStateDetail;
}

void LineListener::onCallStateConferenced(HCALL hCall)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall;
}

void LineListener::onCallStateUnknown(HCALL hCall, DWORD callStateDetail, CallPrivilege::Constant callPrivilege)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << hCall << callStateDetail << callPrivilege;
}


void LineListener::onAddressState(DWORD addressId, AddressState::Constants addressState)
{
    qDebug() << Q_FUNC_INFO << getLineHandle() << addressId << addressState;
}

}
